package com.atm.atmsys.Service.Imp;


import com.atm.atmsys.Mapper.AccountMapper.AccountMapper;
import com.atm.atmsys.Service.AccountService;
import com.atm.atmsys.dto.*;
import com.atm.atmsys.entity.AccountInfo;
import com.atm.atmsys.entity.AdminInfo;
import com.atm.atmsys.entity.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.math.BigDecimal;
import java.security.SecureRandom;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.util.List;

import static com.atm.atmsys.constant.StatusConstant.AccountStatus;

@Service
public class AccountServiceImp implements AccountService {

    @Autowired
    private AccountMapper accountMapper;



    //添加账户
    @Override
    public void addAccount(AccountInfo accountInfo) {
        accountInfo.setAccountTime(LocalDateTime.now());
        accountInfo.setAccountStatus(AccountStatus);
        accountInfo.setAccountPassword(DigestUtils.md5DigestAsHex(accountInfo.getAccountPassword().getBytes()));
        //生成16位卡号
        SecureRandom random = new SecureRandom();
        StringBuilder sb = new StringBuilder(16);
        for (int i = 0; i < 16; i++) {
            sb.append(random.nextInt(10)); // 生成0-9之间的随机数字
        }
        String random16DigitNumber = sb.toString();
        accountInfo.setCardNo(random16DigitNumber);

        accountInfo.setBalance(BigDecimal.valueOf(0.00));

        accountMapper.addAccount(accountInfo);


    }


    //ATM登录
    @Override
    public AccountInfo login(ATMLoginDTO atmLoginDTO) {
        String password = DigestUtils.md5DigestAsHex(atmLoginDTO.getAccountPassword().getBytes());
        atmLoginDTO.setAccountPassword(password);
        AccountInfo accountInfo = new AccountInfo();
        BeanUtils.copyProperties(atmLoginDTO, accountInfo);
        return accountMapper.getByAccountCardNo(accountInfo);
    }

    //获取所有卡号
    @Override
    public List<String> getAllCardNO() {
        return accountMapper.getAllCardNO();
    }

    //根据卡号删除账户
    @Override
    public void deleteAccount(String cardno) {
        accountMapper.deleteAccount(cardno);
    }


    //根据Id更新账户状态
    @Override
    public void updateAccountStatus(AccountStatusDTO accountStatusDTO) {
        accountMapper.updateAccountStatus(accountStatusDTO);
    }

    //根据卡号获取余额
    @Override
    public String getBalanceByCardNO(String cardNo) {
        double balance = accountMapper.getBalanceByCardNO(cardNo);
        DecimalFormat df = new DecimalFormat("0.00");
        String formattedNumber = df.format(balance);
        return formattedNumber;
    }


    //存钱
    @Override
    public void saveMoney(SaveMoneyDTO saveMoneyDTO) {
        accountMapper.saveMoney(saveMoneyDTO);
    }

    //根据卡号获取账户状态
    @Override
    public String getAccountStatusByCardNO(String cardNo) {

        return accountMapper.getAccountStatusByCardNO(cardNo);
    }

    //取钱
    @Override
    public void drawMoney(DrawMoneyDTO drawMoneyDTO) {
         accountMapper.drawMoney(drawMoneyDTO);
    }

    //转移账户
    @Override
    public void transfer(AccountTransDTO accountTransDTO) {
        accountMapper.transfer(accountTransDTO);
    }

    //根据身份证号获取卡号
    @Override
    public List<String> getCardNOByIdentification(String identification) {
        List<String> cardNOList = accountMapper.getCardNOByIdentification(identification);
        return cardNOList;
    }

    //根据卡号判断账户是否存在
    @Override
    public AccountInfo getAccountByCardNO(String cardNo) {
        return accountMapper.getAccountByCardNO(cardNo);
    }

    //分页查询所有账户
    @Override
    public PageResult pageQueryAccount(AccountPageDTO accountPageDTO) {
        PageHelper.startPage(accountPageDTO.getPage(), accountPageDTO.getPageSize());
        Page<AccountInfo> page =accountMapper.pageQueryAccount(accountPageDTO);
        long total = page.getTotal();
        List<AccountInfo> result = page.getResult();
        return new PageResult(total,result);
    }

    //查询用户账户
    @Override
    public PageResult pageQueryUserAccount(YinLianAccountPageDTO yinLianAccountPageDTO) {
        PageHelper.startPage(yinLianAccountPageDTO.getPage(), yinLianAccountPageDTO.getPageSize());
        Page<AccountInfo> page =accountMapper.pageQueryUserAccount(yinLianAccountPageDTO);
        long total = page.getTotal();
        List<AccountInfo> result = page.getResult();
        return new PageResult(total,result);
    }


}
